package com.fenghuajueli.module_user.ui.refund

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.fenghuajueli.lib_pictureselect.entity.SelectMediaEntity
import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo
import com.fenghuajueli.libbasecoreui.feedback.ImageTools
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils
import com.fenghuajueli.module_user.network.model.refund.RefundRequest
import com.fenghuajueli.module_user.network.retrofit.BackgroundNetwork
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

/**
 * @Author: sz_ijj
 * @Date: 2024-04-02 09:33
 **/
class RefundViewModel(val app:Application) : AndroidViewModel(app) {
    private val _feedImgData = MutableStateFlow(listOf<String>())
    val feedImgData: StateFlow<List<String>> = _feedImgData

    private val _questionDetail = MutableStateFlow("")
    val questionDetail: StateFlow<String> = _questionDetail

    private val _canCommit = MutableStateFlow(false)
    val canCommit: StateFlow<Boolean> = _canCommit

    private val _isLoading = MutableSharedFlow<Boolean>(1)
    val isLoading = _isLoading.asSharedFlow()

    init {
        ImageTools.initBsy()
    }

    fun setFeedImgData(data: List<SelectMediaEntity>) = viewModelScope.launch {
        if (data.isEmpty())
            return@launch

        _feedImgData.update {
            it.plus(data.map { newValue ->
                newValue.path
            })
        }
        checkCanCommit()
    }

    fun removeFeedImage(index: Int)= viewModelScope.launch {
        _feedImgData.update {
            it.filterIndexed { i, _ ->
                i != index
            }
        }
        checkCanCommit()
    }

    fun setQuestionDetail(value:String)= viewModelScope.launch {
        _questionDetail.emit(value)
        checkCanCommit()
    }

    private suspend fun checkCanCommit() {
        _canCommit.emit(
            _questionDetail.value.isNotBlank() &&
                _feedImgData.value.isNotEmpty()
        )
    }

    fun commitRefund() = viewModelScope.launch(Dispatchers.Default) {
        runCatching {
            _isLoading.emit(true)
            val result = BackgroundNetwork.commitUserRefund(
                RefundRequest(
                    user_id = UserInfoUtils.getInstance().userInfoEntity.userInfo.user_id,
                    app_id = AppConfigInfo.APPLICATION_ID,
                    refund_reason = _questionDetail.value,
                    payment_voucher = _feedImgData.value.map {
                        imageUpload(it)
                    }.apply {
                        LogUtils.d(this)
                    }
                )
            )

            when(result.code){
                200 -> "提交成功"
                else -> result.msg
            }
        }.onFailure {
            LogUtils.e(it)
            _isLoading.emit(false)
        }.onSuccess {
            ToastUtils.showLong(it)
            _isLoading.emit(false)
        }
    }

    private fun imageUpload(image: String): String {
        return "http://ss.bscstorage.com/ijj-static/"+ImageTools.getUrl(app,image)
    }
}